//
//  AppCacheInfo.swift
//  Exchange
//
//  Created by rui on 2018/4/19.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class AppCacheInfo {
   
    static let `default` = AppCacheInfo()
    
    private struct Cache {
        static let cacheKeyCurrency =  "cacheKeyCurrency"
        static let cacheKeyLanguage = "cacheKeyLanguage"
        static let cacheKeyTheme = "cacheKeyTheme"
    }
    
    ///币种
    var currency: String {
        get {
            if let cur = UserDefaults.standard.string(forKey: Cache.cacheKeyCurrency) {
                return cur
            }
            return "CNY"
        }
        set {
            UserDefaults.standard.set(newValue, forKey: Cache.cacheKeyCurrency)
            UserDefaults.standard.synchronize()
        }
    }
    
    
    ///语言
    var isEnlish: Bool {
        let flag = AppCacheInfo.default.language.contains("en")
        return flag
    }
    
    ///语言
    var language: String {
        get {
            var language = "zh-Hans"
            if let lan = UserDefaults.standard.string(forKey: Cache.cacheKeyLanguage) {
                language = lan
            }else if let languages = UserDefaults.standard.array(forKey: "AppleLanguages"),let lan = languages.first as? String {
                if lan.contains(language) {
                }else {
                    language = lan
                }
            }
            return language
        }
        set {
            UserDefaults.standard.set(newValue, forKey: Cache.cacheKeyLanguage)
            UserDefaults.standard.synchronize()
        }
    }
    ///主题
    var theme: Int {
        get {
           return UserDefaults.standard.integer(forKey: Cache.cacheKeyTheme)
        }
        set {
            UserDefaults.standard.set(newValue, forKey: Cache.cacheKeyTheme)
            UserDefaults.standard.synchronize()
        }
    }
    
    func set(_ value:Any, forKey: String) {
        UserDefaults.standard.set(value, forKey: forKey)
        UserDefaults.standard.synchronize()
    }
    
    func object(forKey: String) -> Any? {
        return UserDefaults.standard.object(forKey:forKey)
    }
}
